package com.example.demo;

import com.example.demo.dao.DemoDao;
import com.example.demo.service.DemoService;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/*
 * 类描述:
 *在第3步的结果集中，使用java代码筛选其中商品数量最大值属于哪天入库，哪个商品类别，并将该入库日期、商品类别、商品数量输出，使用test_dict翻译商品类别名称，输出结果示例：
				[
					{
						tjrq:2022-12-04
						tjlb:'LB003',
						tjlbmc:'足球',
						tjnum:99
					},
					{
						tjrq:2022-12-05
						tjlb:'LB002',
						tjlbmc:'篮球',
						tjnum:99
					}
				]
 *
 * @Author: Administrator
 * @Date: 2023/2/23
 * @Version: 1.0.0
 */
@SpringBootTest
public class Exam4Testor {


    @Autowired
    private DemoService demoService;
    @Autowired
    private DemoDao demoDao;


    @Test
    void m4() {
        Map<String, String> param = new HashMap();
        param.put("startDate", "2022-12-01");
        param.put("endDate", "2022-12-09");

        try {
            List<LinkedHashMap<String, String>> resultD = demoDao.examtest3(param);
            LinkedHashSet<String> datakeyset = new LinkedHashSet<>();

            //获取key
            resultD.forEach(new Consumer<LinkedHashMap<String, String>>() {
                @Override
                public void accept(LinkedHashMap<String, String> smap) {
                    datakeyset.add(String.valueOf(smap.get("tjrq")));
                }
            });


            //拼接数据
            LinkedHashMap<String, Object> slinkedhashmaps = new LinkedHashMap<String, Object>();
            for (String key : datakeyset) {
                LinkedList<Object> objects = new LinkedList<>();

                for (LinkedHashMap<String, String> smap : resultD) {
                    String tjrq = String.valueOf(smap.get("tjrq"));
                    String tjlb = StringUtils.isNotBlank(smap.get("tjlb")) ? smap.get("tjlb") : "";
                    String tjlbmc = StringUtils.isNotBlank(smap.get("tjlbmc")) ? smap.get("tjlbmc") : "";
                    String tjnum = StringUtils.isNotBlank(String.valueOf(smap.get("tjnum"))) ? String.valueOf(smap.get("tjnum")) : "";
                    if (key.equals(tjrq)) {
                        LinkedHashMap<String, Object> temp = new LinkedHashMap<>();
                        temp.put("tjlb", tjlb);
                        temp.put("tjlbmc", tjlbmc);
                        temp.put("tjnum", tjnum);
                        objects.add(temp);
                    }
                }
                slinkedhashmaps.put(key, objects);
            }


        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }

    @Test
    void m5() {
        Map<String, String> param = new HashMap();
        param.put("startDate", "2022-12-01");
        param.put("endDate", "2022-12-09");
        LinkedHashMap<String, Object> rsfind = demoService.examtest3(param);

        //平铺数据
        ArrayList<Map<String, String>> mapslist = mapslist = new ArrayList<>();;
        for(Map.Entry<String,Object> entry : rsfind.entrySet()){
            String key = entry.getKey();
            List<Map<String,String>> valuelist = (List<Map<String,String>>)entry.getValue();

            for (Map<String, String> smap : valuelist) {
                smap.put("tjrq",key);
                mapslist.add(smap);
            }

        }
        //System.out.println(mapslist);

        //比较
        /*Collections.sort(mapslist, new Comparator<Map<String, String>>() {
            @Override
            public int compare(Map<String, String> o1, Map<String, String> o2) {
                BigDecimal b1 = new BigDecimal(o1.get("tjnum"));
                BigDecimal b2 = new BigDecimal(o2.get("tjnum"));
                return b1.compareTo(b2);
            }
        });
        System.out.println("===============");
        System.out.println(mapslist);*/
        List<Map<String, String>> collect = mapslist.stream().sorted(new Comparator<Map<String, String>>() {
            @Override
            public int compare(Map<String, String> o1, Map<String, String> o2) {
                BigDecimal b1 = new BigDecimal(o1.get("tjnum"));
                BigDecimal b2 = new BigDecimal(o2.get("tjnum"));
                return b2.compareTo(b1);
            }
        }).limit(1).collect(Collectors.toList());


    }


}
